<template>
  <div class="page-content">
    <hr-table
      :list-data="dataList"
      :prop-list="contentTableConfig?.propList"
      :list-count="usersCount"
      v-bind="contentTableConfig"
      v-model:page="pageInfo"
    >
      <!-- 1.header中的插槽 -->
      <template #headerHandler>
        <el-button type="primary">新建用户</el-button>
      </template>

      <!-- 2.列中的插槽 -->
      <template #handler>
        <div class="handle-btns">
          <el-button icon="edit" type="text">编辑</el-button>
          <el-button icon="delete" type="text">删除</el-button>
        </div>
      </template>

      <!-- 在page-content中动态插入剩余的插槽 -->
      <template v-for="item in otherPropSlots" :key="item.prop" #[item.slotName]="scope">
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </hr-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useStore } from '@/store'

import HrTable from '@/base-ui/table'

export default defineComponent({
  components: {
    HrTable
  },
  props: {
    contentTableConfig: {
      type: Object,
      require: true
    },
    pageName: {
      type: String,
      required: true
    },
    searchFormData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  setup(props) {
    const store = useStore()

    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    watch(pageInfo, () => getPageData())

    const getPageData = (queryInfo: any = {}) => {
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()

    const dataList = computed(() => store.getters[`system/pageListData`](props.pageName))

    const usersCount = computed(() => store.getters[`system/pageListCount`](props.pageName))

    // 4.获取其他的动态插槽名称
    const otherPropSlots = props.contentTableConfig?.propList.filter((item: any) => {
      if (item.slotName === 'handler') return false
      return true
    })

    return {
      dataList,
      usersCount,
      pageInfo,
      otherPropSlots,
      getPageData
    }
  }
})
</script>

<style scoped>
.page-content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
